import { PrismaService } from '@/modules/prisma/prisma.service';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject } from '@nestjs/common';
import { Cache } from 'cache-manager';
import {
  BadRequestException,
  ConflictException,
  Injectable,
  NotFoundException,
} from '@nestjs/common';

@Injectable()
export class FriendService {
  constructor(
    private prisma: PrismaService,
    @Inject(CACHE_MANAGER) private cacheManager: Cache,
  ) {}

  async getFriends(userId: number) {
    const friends = await this.prisma.friend.findMany({
      where: { userId: userId },
      select: {
        remark: true,
        user: {
          select: {
            userId: true,
            phone: true,
            username: true,
            avatar: true,
          },
        },
      },
    });
    return friends.map((friend) => ({
      ...friend.user,
      remark: friend.remark,
    }));
  }

  async addFriend(userId: number, friendId: number) {
    await 1;
  }

  // async addFriendToGroup(
  //   userId: number,
  //   friendId: number,
  //   groupId: number,
  //   remark: string,
  // ) {
  //   //按道理说搜索得到的人如果是好友会显示已添加
  //   const exists = await this.prisma.friend.findUnique({
  //     where: { userId_friendId: { userId, friendId } },
  //   });
  //   if (exists) throw new ConflictException('好友已存在');

  //   return this.prisma.friend.create({
  //     data: { userId, friendId, groupId, remark },
  //   });
  // }

  async deleteFriend(userId: number, friendId: number) {
    return this.prisma.friend.delete({
      where: { userId_friendId: { userId, friendId } },
    });
  }
}
